package com.vsen.lpr.task;

import com.vsen.lpr.entity.TaskPlan;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author jaxons.he
 * @date 2021/8/22  3:08 下午
 * @description:
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DelayTask implements Delayed {
    private Long expireTime;//到期时间
    private TaskPlan data;//插入的数据


    //对外提供一个创建任务实例的方法
    public static DelayTask buildTask(TaskPlan taskPlan){
        DelayTask delayTask = new DelayTask();
        delayTask.setData(taskPlan);
        delayTask.setExpireTime(taskPlan.getRemindTime().getTime());
        return delayTask;
    }
    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(expireTime-System.currentTimeMillis(),TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return (int)(this.getDelay(TimeUnit.SECONDS)-o.getDelay(TimeUnit.SECONDS));
    }
}
